package com.serein.controller;

import com.serein.domain.User;
import com.serein.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 登录
 *
 * @author SeRein
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    @RequestMapping("/login")
    public Object list(User user) {

        System.out.println(user);
        int res = userService.login(user);
        System.out.println(res);

        if (res == 0) {
            System.out.println("登录失败");
            return "redirect:/index.jsp";
        } else {
            System.out.println("登录成功");
            return "home";
        }
    }

    @RequestMapping("/showUsers")
    public String showUsers(Model model) {
        List<User> usersList = userService.findAll();
        for (User user : usersList) {
            System.out.println(user);
        }
        model.addAttribute("userList", usersList);
        return "showUsers";
    }


    @RequestMapping("/delUser/{id}")
    public String delUser(@PathVariable int id, Model model) {
        System.out.println("执行delUserController");

        int re = userService.delUser(id);
        if (re==1){
            return "test";
        }else {
            return "erro";
        }
    }

    @RequestMapping("/addUsers")
    public String addUsers(){
        System.out.println("addUsers执行");
        userService.addUsers();
        return "showUsers";
    }
}
